#include "testDrive.h"

void operatorTestRun( string str, bool rez, bool expect, int line){
	bool pass = rez == expect;
	ostringstream oss;
	oss << str << "\t result:" << boolalpha << rez << "\texpected:" << boolalpha << expect << "\tpass:" << boolalpha << pass;

	if (pass)
		logger::success(oss.str(), "operatorsTest.txt", line);
	else
		logger::fail(oss.str(), "operatorsTest.txt", line);
}

void operatorsTest(big_number *num1, big_number *num2, bool equel, bool small, int line)
{
	operatorTestRun((*num1).toString() + " == " + (*num2).toString(), (*num1) == (*num2), equel, line);
	operatorTestRun((*num1).toString() + " != " + (*num2).toString(), (*num1) != (*num2), !equel, line);
	operatorTestRun((*num1).toString() + " < " + (*num2).toString(), (*num1)<(*num2), small, line);
	operatorTestRun((*num1).toString() + " > " + (*num2).toString(), (*num1)>(*num2), !small && !equel, line);
	operatorTestRun((*num1).toString() + " <= " + (*num2).toString(), (*num1) <= (*num2), small || equel, line);
	operatorTestRun((*num1).toString() + " >= " + (*num2).toString(), (*num1) >= (*num2), !small, line);
}

void compareOperatorsParser(string *arr, big_number **a, big_number **b, bool *equal, bool *small){
	if ( arr[0].compare("i") == 0)
		*a = new big_integer(arr[1]);
	else
		*a=  new big_number(arr[1]);

	if ( arr[2].compare("i") == 0)
		*b = new big_integer(arr[3]);
	else
		*b =  new big_number(arr[3]);

	*equal = arr[4].compare("1") == 0;
	*small = arr[5].compare("1") == 0;	
}

void compareOperatorsTest(){
	PNode tmp, ptr = readFile("operatorsTest.txt");
	string arr[6];
	big_number *a, *b;
	bool equal, small;
	printf("Start compare operators test - \n");
	while ( ptr != NULL ){
		try { 
			splitByComma(arr, ptr->line);
			compareOperatorsParser(arr, &a, &b, &equal, &small);
			operatorsTest(a, b, equal, small, ptr->lineNumber);
		} 
		catch( const std::exception &e) {
			logger::exception(ptr->line, "operatorsTest.txt", ptr->lineNumber);
			cout << e.what() << endl;
		}
		tmp = ptr;
		ptr = ptr->next;
		freeNode(tmp);
	}
}